<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="channels.perfectNumber" placeholder="用户ID" style="width: 200px; margin-right: 20px"
        class="filter-item" />
      <el-input v-model="channels.keyword" placeholder="内容" style="width: 200px; margin-right: 20px"
        class="filter-item" />
      <el-select v-model="channels.status" placeholder="状态" clearable class="filter-item"
        style="width: 200px; margin-right: 20px">
        <el-option v-for="item in calendarTypeOptions" :key="item.id" :label="item.nickname" :value="item.id" />
      </el-select>
      <el-select v-model="channels.autoStatus" placeholder="机审结果" clearable class="filter-item"
        style="width: 200px; margin-right: 20px">
        <el-option v-for="item in calendarTypeOptions2" :key="item.id" :label="item.nickname" :value="item.id" />
      </el-select>

      <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 20px"
        @click="handleFilter();channels.page=1">
        搜索
      </el-button>
    </div>


    <div class="batch">
      <el-button class="filter-item" type="primary" icon="el-icon-close" @click="toggleSelection(2)">
        批量拒绝
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-check" @click="toggleSelection(1)">
        批量通过
      </el-button>
    </div>
    <el-table ref="multipleTable" :data="list" stripe style="width: 1640px" border
      @selection-change="handleSelectionChange" :row-key="getRowKeys">
      <el-table-column type="selection" width="55" align="center" :reserve-selection="true"
        :selectable="checkboxInit" />
      <el-table-column prop="perfect_number" label="ID" width="100" align="center" />
      <el-table-column prop="nickname" label="发布人" width="100" align="center">
        <template slot-scope="scope">
          <span>
            {{scope.row.nickname+'('+scope.row.perfect_number+')'}}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="举报动态" width="300" align="center">
        <template slot-scope="scope">
          <!-- 文字 -->
          <el-tooltip class="item" effect="dark" :content="scope.row.r_content" placement="top">
            <span
              style="text-overflow: -o-ellipsis-lastline; overflow: hidden;text-overflow: ellipsis;display: -webkit-box; -webkit-line-clamp: 2;  line-clamp: 2;-webkit-box-orient: vertical;">
              {{scope.row.r_content}}
            </span>
          </el-tooltip>

          <div class="demo-image__preview">
            <el-image :class="'my-img image_list'+scope.row.pic_url.length" v-for="item,index in scope.row.pic_url"
              :key="index" :src="item" :preview-src-list="scope.row.pic_url" @click.stop="handleClickItem">
            </el-image>
          </div>
          <!-- 视频 -->
          <div style="display:flex;justify-content: center;align-items: center;margin-top:10px"
            v-if="scope.row.video_url">
            <video :preload="preload" :poster="scope.row.video_image" width="60%" align="center"
              @click="openVideo(scope.row.video_url)" :controls="controls" :autoplay="autoplay">
            </video>
            <img style="width:35px;height:35px;position:absolute;cursor: pointer;" src="../../icons/image/play.png"
              @click="openVideo(scope.row.video_url)" alt="" />
          </div>
          <!-- 音频 -->
          <audio v-if="scope.row.voice" ref="audio" :src="scope.row.voice" controls="controls" style="width:100%"
            @pause="onPause" @play="onPlay" @timeupdate="onTimeupdate" @loadedmetadata="onLoadedmetadata" />
        </template>
      </el-table-column>

      <el-table-column prop="content" label="举报内容" align="center">
        <template slot-scope="scope">
          <span
            style="text-overflow: -o-ellipsis-lastline; overflow: hidden;text-overflow: ellipsis;display: -webkit-box; -webkit-line-clamp: 3;  line-clamp: 3;-webkit-box-orient: vertical;">
            {{scope.row.content}}
          </span>
        </template>
      </el-table-column>

      <el-table-column prop="image" label="举报截图" align="center" width="250">
        <template slot-scope="scope" align="center">
          <div class="demo-image__preview">
            <el-image class="my-img image_list3" v-for="item,index in scope.row.image" :key="index" :src="item"
              :preview-src-list="scope.row.image" @click.stop="handleClickItem">
            </el-image>
          </div>
        </template>
      </el-table-column>

      <el-table-column prop="desc" label="类型" align="center" />
      <el-table-column prop="add_time" label="提交时间" align="center" width="200" />
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope" align="center">
          <span v-if="scope.row.status == '0'">审核中</span>
          <span v-else-if="scope.row.status == '1'">通过</span>
          <span v-else>拒绝</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200" align="center">
        <template slot-scope="scope" align="center">
          <el-button :disabled="scope.row.status != '0'" type="primary" size="small"
            @click="adopt(scope.row.id,'1',scope.row.type)">通过
          </el-button>
          <el-button :disabled="scope.row.status != '0'" type="button" size="small"
            @click="adopt(scope.row.id,'2',scope.row.type)">拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="channels.page" :limit.sync="channels.pageSize"
      @pagination="handleFilter" />

    <el-dialog title="处理结果" :visible.sync="dialogVisible" width="400px" center>
      <div class="center_radio">
        <span>冻结账号:</span>
        <div>
          <el-radio v-model="is_lock" label="1">是</el-radio>
          <el-radio v-model="is_lock" label="0">否</el-radio>
        </div>
      </div>
      <div class="center_radio">
        <span>是否禁言:</span>
        <div>
          <el-radio v-model="is_nospeak" label="1">是</el-radio>
          <el-radio v-model="is_nospeak" label="0">否</el-radio>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" style="width: 100%" @click="AddWhitelist">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="videoDialog" width="70%" center class="videoPlayer">
      <div>
        <video-player style="width: 100%;height:500px" class="video-player vjs-custom-skin" ref="videoPlayer"
          :playsline="false" :options="playerOptions" @play="onPlayerPlay($event)" @pause="onPlayerPause($event)"
          @ended="onPlayerEnded($event)" @waiting="onPlayerWaiting($event)" @playing="onPlayerPlaying($event)"
          @loadeddata="onPlayerLoadeddata($event)" @timeupdate="onPlayerTimeupdate($event)"
          @canplay="onPlayerCanplay($event)" @canplaythrough="onPlayerCanplaythrough($event)"
          @statechanged="playerStateChanged($event)" @ready="playerReadied"></video-player>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import Cookies from "js-cookie";
  import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
  import {
    encryption2,
    fileSuffixTypeUtil
  } from "@/api/index";


  export default {
    name: "ComplexTable",
    components: {
      Pagination,
    },
    data() {
      return {
        // videoImg: 'http://img.xunyinjiaoyou.com/upload/avatar/202107/21/2428_1626836173.jpg',
        preload: 'auto', //  建议浏览器是否应在<video>加载元素后立即开始下载视频数据。
        controls: false, // 确定播放器是否具有用户可以与之交互的控件。没有控件，启动视频播放的唯一方法是使用autoplay属性或通过Player API。
        autoplay: '',
        videoDialog: false,
        playerOptions: {
          sources: [{
            src: ''
          }]
        },
        is_lock: '0',
        is_nospeak: '0',
        dialogVisible: false,
        srcList: [],
        status_: '',
        fileSuffixTypeUtil: fileSuffixTypeUtil,
        channels: {
          perfectNumber: "", // 用户ID
          keyword: "", // 昵称
          status: "", // 审核状态
          autoStatus: "", // 机审状态
          page: 1,
          pageSize: 10,
        },
        calendarTypeOptions: [{
            id: 0,
            nickname: "待审核"
          },
          {
            id: 1,
            nickname: "通过"
          },
          {
            id: 2,
            nickname: "拒绝"
          },
        ],
        calendarTypeOptions2: [{
            id: 0,
            nickname: "待审核"
          },
          {
            id: 1,
            nickname: "通过"
          },
          {
            id: 2,
            nickname: "拒绝"
          },
        ],
        total: 0,
        list: [],
        multipleSelection: [], //批量对象
        audio: {},
      };
    },
  created() {
      this.handleFilter();
    },
    beforeRouteEnter(to, from, next) {
      if (from.name == "UserQuery") {
        next((vm) => {
          // 通过 `vm` 访问组件实例
          vm.functionA();
        });
      } else {
        next();
      }
    },
    methods: {
      functionA() {
        this.channels.status = 0;
        this.handleFilter();
      },
      checkboxInit(row, index) {
        if (row.status != '0') {
          return false
        } else {
          return true
        }
      },
      handlePriveImg(row) {
        this.srcList.push(row); //将后端的这一行的数据push进数组中
        this.$refs.myImg.clickHandler();
      },
      getRowKeys(row) {
        return row.id
      },
      toggleSelection(rows) {
      if(this.multipleSelection.length){
         this.adopt(this.multipleSelection, rows)
        }else{
          if (rows == '1') {
            this.list.forEach(row => {
              if (row.status == '0') {
                this.$refs.multipleTable.toggleRowSelection(row, true);
              }
            });
            this.status_ = rows
            if (this.multipleSelection.length) {
              this.adopt(this.multipleSelection, rows)
            } else {
              this.$message({
                message: `请选择批量对象`,
                type: "success"
              });
            }
          } else {
            this.list.forEach(row => {
              if (row.status == '0') {
                this.$refs.multipleTable.toggleRowSelection(row, true);
              }
            });
            this.status_ = rows
            if (this.multipleSelection.length) {
              this.adopt(this.multipleSelection, rows)
            } else {
              this.$message({
                message: `请选择批量对象`,
                type: "success"
              });
            }
          }
        }
      },
      // 获取图片遮罩层dom
     handleClickItem() {
        setTimeout(function () {
          let domImageMask = document.querySelector(".el-image-viewer__wrapper");
          if (!domImageMask) {
            return;
          }
          domImageMask.addEventListener("click", (e) => {
            if (e.target.parentNode.className == 'el-image-viewer__actions__inner' || e.target.parentNode.className == 'el-image-viewer__btn el-image-viewer__next'  || e.target.parentNode.className == 'el-image-viewer__btn el-image-viewer__prev') {
              return; //如果点击底部菜单，不关闭
            }
            // 点击遮罩层时调用关闭按钮的 click 事件
            document.querySelector(".el-image-viewer__close").click();
            document.querySelector('body').setAttribute('style', 'overflow:auto;')
          });
        }, 300)
      },
      handleSelectionChange(val) {
        this.multipleSelection = val.map(item => {
          return item.id;
        })
        this.multipleSelection = this.multipleSelection.join(",")
      },
      adopt(id, status_, type) {
        this.is_lock = '0';
        this.is_nospeak = '0';
        this.multipleSelection = id;
        this.status_ = status_
        if (type == '1' && status_ == '1') {
          this.dialogVisible = true;
        } else {
          this.AddWhitelist()
        }
      },
      // 确认禁言
      AddWhitelist() {
        const arys = {
          ids: this.multipleSelection,
          status: this.status_,
          is_lock: this.is_lock,
          is_nospeak: this.is_nospeak,
        };
        const url = "/audit/reportAudit";
        encryption2(arys, url).then((res) => {
          if (res.data.status === 0) {
            this.$refs.multipleTable.clearSelection();
            this.$message({
              message: res.data.text,
              type: "success",
            });
            this.handleFilter()
          } else {
            this.$message({
              message: res.data.text,
              type: "error",
            });
          }
          this.dialogVisible = false
        });
      },


      // 查询
      handleFilter() {
        const arys = {
          perfectNumber: this.channels.perfectNumber, // 用户ID
          keyword: this.channels.keyword, // 昵称
          status: this.channels.status, // 审核状态
          autoStatus: this.channels.autoStatus, // 机审状态
          page: this.channels.page,
          pageSize: this.channels.pageSize,
        };
        const url = "/audit/recordReportList";
        encryption2(arys, url).then((res) => {
          if (res.data.status === 0) {
            this.list = res.data.result.list;
            this.total = res.data.result.total;
          } else {
            this.$message({
              message: res.data.text,
              type: "error",
            });
          }
        });
      },
      openVideo(row) {
        this.videoDialog = true;
        this.row = row;
        this.playerOptions.sources[0].src = row;
      },
      // 播放回调
      onPlayerPlay(player) {},

      // 暂停回调
      onPlayerPause(player) {},

      // 视频播完回调
      onPlayerEnded($event) {},

      // DOM元素上的readyState更改导致播放停止
      onPlayerWaiting($event) {},

      // 已开始播放回调
      onPlayerPlaying($event) {},

      // 当播放器在当前播放位置下载数据时触发
      onPlayerLoadeddata($event) {},

      // 当前播放位置发生变化时触发。
      onPlayerTimeupdate($event) {},

      //媒体的readyState为HAVE_FUTURE_DATA或更高
      onPlayerCanplay(player) {},

      //媒体的readyState为HAVE_ENOUGH_DATA或更高。这意味着可以在不缓冲的情况下播放整个媒体文件。
      onPlayerCanplaythrough(player) {},

      //播放状态改变回调
      playerStateChanged(playerCurrentState) {},

      //将侦听器绑定到组件的就绪状态。与事件监听器的不同之处在于，如果ready事件已经发生，它将立即触发该函数。。
      playerReadied(player) {},

      // 当音频播放
      onPlay() {
        this.audio.playing = true
      },
      // 当音频暂停
      onPause() {
        this.audio.playing = false
      },
      // 当timeupdate事件大概每秒一次，用来更新音频流的当前播放时间
      onTimeupdate(res) {
        this.audio.currentTime = res.target.currentTime
      },
      // 当加载语音流元数据完成后，会触发该事件的回调函数
      // 语音元数据主要是语音的长度之类的数据
      onLoadedmetadata(res) {
        this.audio.maxTime = parseInt(res.target.duration)
      }
    },
  };
</script>
<style scoped>
  .filter-container .filter-item {
    margin-bottom: 0;
  }

  .el-table--border {
    border-right: 1px solid #dfe6ec;
  }

  .el-alert {
    width: 1200px;
  }

  .filter-container {
    margin-top: 20px;
  }

  .center_radio {
    width: 88%;
    justify-content: center;
    display: flex;
    margin-bottom: 40px;
  }

  .center_radio>div {
    display: flex;
    flex: 1;
    justify-content: space-around;
  }

  .batch {
    width: 1640px;
    margin: 10px 0;
    display: flex;
    flex-direction: row-reverse;
  }

  .batch .filter-item {
    margin-left: 20px;
  }

  .my-img {
    width: 120px;
    padding: 10px;
    margin: 0 auto;
  }

  .image_list1 {
    width: 120px;
    padding: 10px;
  }

  .image_list2 {
    width: 100px;
    padding: 10px;
  }

  .image_list3 {
    width: 70px;
    height: 70px;
    padding: 10px;
  }


    .videoPlayer /deep/ .video-player {
    width: 1000px;
    height: 600px;
    overflow: hidden;
  }

  .videoPlayer /deep/ .video-js {
    width: 1000px;
    height: 600px;
  }

  .videoPlayer /deep/ .vjs-tech {
    width: 1000px;
    height: 600px;
  }



  
</style>